package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 下单接口
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String>submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("支付成功");
    }

    @GetMapping("/userPage")
    public R<Page>pageR(Integer page,Integer pageSize){
        Long getid = BaseContext.getid();
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,getid);


        ordersService.page(ordersPage,queryWrapper);
//        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        List<Orders> list = ordersPage.getRecords();
        ArrayList<OrdersDto> ordersDtos = new ArrayList<>();

        for (Orders orders : list) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);

            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            log.info(orders.getId().toString());
            queryWrapper1.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list1 = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(list1);
            ordersDtos.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }

    /**
     * 分页查询订单信息，同时查询出用户名称
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page>page(Integer page,Integer pageSize,String number,String beginTime,String endTime){
        log.info("page={},pagesize={}",page,pageSize);
        //分页查询将页数和查询个数放入方法中
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number);
        queryWrapper.between(StringUtils.isNotBlank(beginTime)||StringUtils.isNotBlank(endTime),
                Orders::getCheckoutTime,beginTime,endTime);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //调用page方法
        ordersService.page(ordersPage,queryWrapper);
        //拷贝将订单表格数据拷贝到订单DTO中
        BeanUtils.copyProperties(ordersPage,ordersDtoPage);
        //获取订单表中所有数据
        List<Orders> Orderslist = ordersPage.getRecords();
        //new一个泛型为OrdersDto的数组，用来添加后续的对象
        ArrayList<OrdersDto> ordersDtoslist = new ArrayList<>();
        for (Orders record : Orderslist) {
            OrdersDto ordersDto = new OrdersDto();
            //将订单表中所有数据拷贝到新的实体类中
            BeanUtils.copyProperties(record,ordersDto);
            //获取订单表中的用户ID
            Long userId = record.getUserId();
            //调用用户表查询用户ID，并返回对象
            User byId = userService.getById(userId);
            //获取用户ID
            Long id = byId.getId();
            //获取用户名字
            String name = byId.getName();
            //设置用户ID
            ordersDto.setUserId(id);
            //设置用户名字
            ordersDto.setUserName(name);
            ordersDtoslist.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtoslist);
        return R.success(ordersDtoPage);
    }


    @PutMapping
    public R<Orders>put(@RequestBody Orders orders){
        log.info(orders.toString());
        if (orders.getStatus()!=null&&orders.getStatus()==3) {
            orders.setStatus(3);
            ordersService.updateById(orders);
        }else if (orders.getStatus()==4){
            orders.setStatus(4);
            ordersService.updateById(orders);
        }
        return R.success(orders);
    }
}
